home *** CD-ROM | disk | FTP | other *** search
- #include <Memory.h>
- #include <Fonts.h>
- #include <Resources.h>
- #include <QuickDraw.h>
- #include <FixMath.h>
- #include <ToolUtils.h>
-
- #include "access library.h"
-
- Handle GetNamedSfntHandle(unsigned char* name, short styleWord)
- {
- Handle fond = GetNamedResource('FOND', name);
- if (fond && !ResError())
- { FamRec* frec = (FamRec*)*fond;
- register short* assoc = (short*)(frec + 1);
- register short sfntID = 0;
- register count = *assoc++;
-
- while (count-- >= 0 && !sfntID)
- { if (*assoc++ == 0) /* size == 0 means sfnt */
- if (*assoc++ == styleWord)
- sfntID = *assoc;
- else
- assoc++;
- else
- assoc += 2;
- }
- if (sfntID)
- { Handle sfnt = GetResource('sfnt', sfntID);
- if (sfnt && !ResError())
- return sfnt;
- }
- }
- Debugger();
- return 0;
- }
-
- Handle GetSfntHandle(short fondID, register short styleWord)
- {
- Handle fond = GetResource('FOND', fondID);
- if (fond && !ResError())
- { FamRec* frec = (FamRec*)*fond;
- register short* assoc = (short*)(frec + 1);
- register short sfntID = 0;
- register count = *assoc++;
-
- while (count-- >= 0 && !sfntID)
- { if (*assoc++ == 0) /* size == 0 means sfnt */
- if (*assoc++ == styleWord)
- sfntID = *assoc;
- else
- assoc++;
- else
- assoc += 2;
- }
- if (sfntID)
- { Handle sfnt = GetResource('sfnt', sfntID);
- if (sfnt && !ResError())
- return sfnt;
- }
- }
- Debugger();
- return 0;
- }
-
- FontError InitGlyphOutline(GlyphOutline* out)
- {
- out->contourCount = 0;
- out->pointCount = 0;
- out->endPoints = (short**)NewHandle(0);
- out->onCurve = (Byte**)NewHandle(0);
- out->x = (Fixed**)NewHandle(0);
- out->y = (Fixed**)NewHandle(0);
- }
-
- FontError KillGlyphOutline(GlyphOutline* out)
- {
- DisposHandle((Handle)out->endPoints);
- DisposHandle((Handle)out->onCurve);
- DisposHandle((Handle)out->x);
- DisposHandle((Handle)out->y);
-
- return fNoError;
- }
-
- void LockGlyphOutline(GlyphOutline* out)
- {
- HLock((Handle)out->endPoints);
- HLock((Handle)out->onCurve);
- HLock((Handle)out->x);
- HLock((Handle)out->y);
- }
-
- void UnlockGlyphOutline(GlyphOutline* out)
- {
- HUnlock((Handle)out->endPoints);
- HUnlock((Handle)out->onCurve);
- HUnlock((Handle)out->x);
- HUnlock((Handle)out->y);
- }
-
- void MoveGlyphOutline(GlyphOutline* out, Fixed xDelta, Fixed yDelta)
- {
- Fixed* x = *out->x;
- Fixed* y = *out->y;
- short count = out->pointCount;
-
- for (--count; count >= 0; --count)
- { *x++ += xDelta;
- *y++ += yDelta;
- }
- out->origin.x += xDelta;
- out->origin.y += yDelta;
- }
-
- void MoveToGlyphOutline(GlyphOutline* out, Fixed xCoord, Fixed yCoord)
- {
- MoveGlyphOutline(out, xCoord - out->origin.x, yCoord - out->origin.y);
- }
-
- void ScaleGlyphOutline(GlyphOutline* out, Fixed xScale, Fixed yScale)
- {
- Fixed* x = *out->x;
- Fixed* y = *out->y;
- short count = out->pointCount;
-
- for (--count; count >= 0; --count)
- { *x = FixMul( *x, xScale );
- x++;
- *y = FixMul( *y, yScale );
- y++;
- }
- out->origin.x = FixMul( out->origin.x, xScale );
- out->origin.y = FixMul( out->origin.y, yScale );
- out->advance.x = FixMul( out->advance.x, xScale );
- out->advance.y = FixMul( out->advance.y, yScale );
- }
-
- void InitMatrix(Matrix mat)
- {
- mat[0][0] = mat[1][1] = mat[2][2] = 0x10000;
- mat[0][1] = mat[0][2] = mat[1][0] = mat[1][2] = mat[2][0] = mat[2][1] = 0;
- }
-
- void MapGlyphOutline(GlyphOutline* out, Matrix mat)
- {
- Fixed* x = *out->x;
- Fixed* y = *out->y;
- short count = out->pointCount;
- Fixed transX = mat[2][0] - out->origin.x;
- Fixed transY = mat[2][1] - out->origin.y;
-
- for (--count; count >= 0; --count)
- { Fixed* m0 = &mat[0][0];
- Fixed* m1 = &mat[1][0];
- Fixed xTemp = *x;
- Fixed yTemp = *y;
-
- *x = FixMul(*m0++, xTemp) + FixMul(*m1++, yTemp) + transX;
- *y = FixMul(*m0++, xTemp) + FixMul(*m1++, yTemp) + transY;
-
- if (*m0 || *m1 || mat[2][2] != 0x10000)
- { Fixed tmp = FracMul(*m0, xTemp) + FracMul(*m1, yTemp);
- tmp += mat[2][2];
- if (tmp && tmp != 0x10000)
- { *x = FixDiv(*x, tmp);
- *y = FixDiv(*y, tmp);
- }
- }
- x++;
- y++;
- }
- }
-
- #define APPENDHANDLE(a,b) AppendHandle((Handle)a, (Handle)b)
-
- void AppendHandle(Handle dst, Handle extra)
- {
- long dstSize = GetHandleSize(dst);
- long extraSize = GetHandleSize(extra);
-
- SetHandleSize(dst, dstSize + extraSize);
- if (MemError())
- Debugger();
- else
- BlockMove(*extra, *dst + dstSize, extraSize);
- }
-
- /* a += b
- */
- void AppendGlyphOutline(GlyphOutline* a, GlyphOutline* b)
- {
- APPENDHANDLE(a->endPoints, b->endPoints);
- { short* p = *a->endPoints + a->contourCount;
- short* endp = p + b->contourCount;
- short newFirstPoint = a->contourCount ? p[-1] + 1 : 0;
- for (; p < endp; p++)
- *p = *p + newFirstPoint;
- }
- a->contourCount += b->contourCount;
- a->pointCount += b->pointCount;
- APPENDHANDLE(a->onCurve, b->onCurve);
- APPENDHANDLE(a->x, b->x);
- APPENDHANDLE(a->y, b->y);
- }
-
- void FrameText( unsigned char* text, FixPoint* scale, int mark )
- {
- FixPoint pen;
- paths* myPath = Text2Paths( text, scale, &pen );
-
- if (myPath)
- { FramePaths(myPath, true);
- if (mark)
- MarkPaths( myPath );
- DisposePaths(myPath);
- fmoveto( pen.x, pen.y );
- }
- }
-
- paths* Text2Paths( unsigned char* text, FixPoint* scale, FixPoint* pen )
- {
- unsigned state;
- Handle sfnt = GetSfntHandle( qd.thePort->txFont, qd.thePort->txFace );
- paths* compositPath = 0;
-
- if (!sfnt)
- return 0;
-
- state = HGetState( sfnt );
- HNoPurge( sfnt );
-
- { GlyphOutline out;
- Fixed originX = ff(qd.thePort->pnLoc.h);
- Fixed originY = ff(qd.thePort->pnLoc.v);
- Fixed xScale = scale->x * qd.thePort->txSize;
- Fixed yScale = scale->y * qd.thePort->txSize;
- int i;
-
- InitGlyphOutline( &out );
- for (i = 1; i <= text[0]; i++)
- { long glyphIndex = GetCharGlyphIndex( sfnt, text[i] );
- GetGlyphOutline( sfnt, glyphIndex, &out, 0 );
- ScaleGlyphOutline( &out, xScale, yScale );
- MoveToGlyphOutline( &out, originX, originY );
-
- { paths* p = OutlineToPaths( &out );
- compositPath = AppendPaths( compositPath, p );
- DisposePaths(p);
- }
- originX += out.advance.x;
- originY += out.advance.y;
- }
- KillGlyphOutline( &out );
-
- if (pen)
- { pen->x = originX;
- pen->y = originY;
- }
- }
- HSetState( sfnt, state );
-
- return compositPath;
- }
-
- static long* PackControlBits(long* p, Byte* onCurve, long count);
- static long* PackControlBits(long* p, Byte* onCurve, long count)
- {
- unsigned long mask = 0x80000000;
-
- *p = 0;
- while (count--)
- { if (!mask)
- { mask = 0x80000000;
- *++p = 0;
- }
- if (!*onCurve++)
- *p |= mask;
- mask >>= 1;
- }
- return p + 1;
- }
-
- paths* OutlineToPaths(GlyphOutline* out)
- {
- long size, *p, *origP;
-
- size = sizeof(long); /* paths.contours */
-
- { long i, sp = 0;
- for (i = 0; i < out->contourCount; i++)
- { long pts = (*out->endPoints)[i] - sp + 1;
- size += sizeof(long); /* path.vectors */
- size += (pts + 31 >> 5) << 2; /* path.controlBits */
- size += pts << 3; /* path.vector[] */
- sp = (*out->endPoints)[i] + 1;
- }
- }
-
- origP = p = (long*)NewPtr( size );
- if (!p || MemError()) Debugger();
-
- *p++ = out->contourCount;
- { long i, sp = 0;
- Fixed* x = *out->x;
- Fixed* y = *out->y;
- short* ep = *out->endPoints;
- Byte* onCurve = *out->onCurve;
- for (i = 0; i < out->contourCount; i++)
- { long pts = *ep - sp + 1;
- *p++ = pts;
- p = PackControlBits(p, onCurve, pts);
- onCurve += pts;
- while (pts--)
- { *p++ = *x++;
- *p++ = *y++;
- }
- sp = *ep++ + 1;
- }
- }
- return (paths*)origP;
- }
-
-